﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using LsMisCommon;

namespace card
{
    public partial class STModifyForm : Form
    {
        #region==========船名下拉列表项的类定义=============
        class ListItem : System.Object
        {
            private string _ShipName = string.Empty;
            private string _CargoName = string.Empty;
            private string _StackName = string.Empty;
            private string _BillNO = string.Empty;
            private string _CargonNO = string.Empty;

            private string _CkindNO = string.Empty;
            private string _GetOwner = string.Empty;
            private string _Package = string.Empty;
            private string _ShipNO = string.Empty;

            public string CkindNO
            {
                get { return this._CkindNO; }
                set { this._CkindNO = value; }
            }
            public string GetOwner
            {
                get { return this._GetOwner; }
                set { this._GetOwner = value; }
            }
            public string Package
            {
                get { return this._Package; }
                set { this._Package = value; }
            }
            public string ShipNO
            {
                get { return this._ShipNO; }
                set { this._ShipNO = value; }
            }

            public string ShipName
            {
                get { return this._ShipName; }
                set { this._ShipName = value; }
            }

            public string CargoName
            {
                get { return this._CargoName; }
                set { this._CargoName = value; }
            }

            public string StackName
            {
                get { return this._StackName; }
                set { this._StackName = value; }
            }

            public string BillNO
            {
                get { return this._BillNO; }
                set { this._BillNO = value; }
            }

            public string CargonNO
            {
                get { return this._CargonNO; }
                set { this._CargonNO = value; }
            }

            public string DisplayName
            {
                get { return this._BillNO + "--" + this._StackName + "--" + this._ShipName + "--" + this._CargoName; }
                //set { this._CargonNO = value; }
            }

            public ListItem(string bill_no, string stack_name, string ship_name, string cargo_name, string cargo_no, string ckind_no, string get_owner, string package, string ship_no)
            {
                this._ShipName = ship_name;
                this._CargoName = cargo_name;
                this._StackName = stack_name;
                this._BillNO = bill_no;
                this._CargonNO = cargo_no;
                //private string _CkindNO = string.Empty;
                //private string _GetOwner = string.Empty;
                //private string _Package = string.Empty;
                //private string _ShipNO = string.Empty;
                this._CkindNO = ckind_no;
                this._GetOwner = get_owner;
                this._Package = package;
                this._ShipNO = ship_no;
            }
            public override string ToString()
            {
                return this._BillNO + "--" + this._StackName + "--" + this._ShipName + "--" + this._CargoName;
            }
        }
        #endregion

        #region==========流向下拉列表项的类定义=============
        class ListItem_LX : System.Object
        {
            public string TDH { get; set; }
            public string CARGONO { get; set; }
            public string CARGONAME { get; set; }
            public string STACKNAME { get; set; }
            public string SHIPNO { get; set; }
            public string SHIPNAME { get; set; }
            public string BILLNO { get; set; }
            public string SHIPPER_NAME { get; set; }
            public string GETOWNER { get; set; }

            public string DisplayName
            {
                get
                {
                    return this.TDH + "--" + this.BILLNO + "--" + this.STACKNAME + "--" + this.SHIPNAME + "--" + this.CARGONAME;
                }
            }

            public ListItem_LX(string _tdh, string _cargoNo, string _cargoName, string _stackName, string _shipNo, string _shipName, string _billNo, string _shipperName, string _getOwner)
            {
                this.TDH = _tdh;
                this.CARGONO = _cargoNo;
                this.CARGONAME = _cargoName;
                this.STACKNAME = _stackName;
                this.SHIPNO = _shipNo;
                this.SHIPNAME = _shipName;
                this.BILLNO = _billNo;
                this.SHIPPER_NAME = _shipperName;
                this.GETOWNER = _getOwner;
            }
        }
        #endregion

        public static WebDG.Service ws = new card.WebDG.Service();

        public string ShipperName
        {
            get
            {
                string shipper = "";
                if (this.comboBox_owner.SelectedValue != null)
                {
                    shipper = this.comboBox_owner.SelectedValue.ToString().Trim();
                }
                else
                {
                    shipper = this.comboBox_owner.Text.ToString().Trim();
                }
                return shipper;
            }
        }
        public string LXH
        {
            get
            {
                string lxh = string.Empty;
                if (this.comboBoxLXH.SelectedValue != null)
                {
                    lxh = this.comboBoxLXH.SelectedValue.ToString().Trim();
                }
                else
                {
                    lxh = this.comboBoxLXH.Text.ToString().Trim();
                }
                return lxh;
            }
        }
        public string ShipName
        {
            get
            {
                string ship_name = "";
                if (this.comboBox_shipname.SelectedValue != null)
                {
                    ship_name = this.comboBox_shipname.SelectedValue.ToString().Trim();
                }
                else
                {
                    ship_name = this.comboBox_shipname.Text.ToString().Trim();
                }
                return ship_name;
            }
        }
        public string HC
        {
            get
            {
                return this.textBox_hc.Text.ToString();
            }
        }
        public string BillNo
        {
            get
            {
                return this.textBox_billNO.Text.ToString();
            }
        }
        public string CargoName
        {
            get
            {
                string cargoName = "";
                if (this.comboBox_cargoName.SelectedValue != null)
                    cargoName = this.comboBox_cargoName.SelectedValue.ToString();
                else
                    cargoName = this.comboBox_cargoName.Text;
                return cargoName;
            }
        }
        public string CargoNo
        {
            get
            {
                return this.textBox_cargoNo.Text.ToString();
            }
        }
        public string StackName
        {
            get
            {
                return this.textBox_stackName.Text.ToString();
            }
        }
        public string Pack
        {
            get
            {
                return this.textBoxPackage.Text.ToString();
            }
        }
        public string Station
        {
            get
            {
                return this.textBoxStation.Text;
            }
        }
        public string Comment
        {
            get
            {
                return this.textBox_comment.Text.ToString();
            }
        }

        public STModifyForm()
        {
            InitializeComponent();
        }

        private bool _hasImport = false;

        public STModifyForm(bool hasImport, string owner, string lxh, string shipname, string hc, string billno, string cargoname, string cargono, string stackname, string pack, string station, string comment)
            : this()
        {
            Init();
            this._hasImport = hasImport;
            SetDefaultValue(owner, lxh, shipname, hc, billno, cargoname, cargono, stackname, pack, station, comment);
        }

        private void Init()
        {
            this.comboBox_owner.DataSource = Session.Shippers.Tables[0];
            this.comboBox_owner.ValueMember = "SHIPPER_NAME";
            this.comboBox_owner.DisplayMember = "DESCRIPTION";

            this.comboBox_cargoName.DataSource = Session.Cargo_names.Tables[0];
            this.comboBox_cargoName.ValueMember = "CARGO_NAM";
            this.comboBox_cargoName.DisplayMember = "DISPLAYNAME";

            string sql_ship_names = "select SHIPNAME,CARGONAME,CARGONO,BILLNO,STACKNAME,CKINDNO,GETOWNER,PACKAGE,SHIPNO from v_ship_tcc";
            DataSet ds_ship_name = ws.getDataSet_Serv(sql_ship_names);
            DataTable dt = ds_ship_name.Tables[0];
            List<ListItem> items2 = new List<ListItem>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string billNo = dt.Rows[i]["BILLNO"].ToString();
                string stackName = dt.Rows[i]["STACKNAME"].ToString();
                string shipName = dt.Rows[i]["SHIPNAME"].ToString();
                string cargoName = dt.Rows[i]["CARGONAME"].ToString();
                string cargoNo = dt.Rows[i]["CARGONO"].ToString();

                string ckind_no = dt.Rows[i]["CKINDNO"].ToString();
                string get_owner = dt.Rows[i]["GETOWNER"].ToString();
                string package = dt.Rows[i]["PACKAGE"].ToString();
                string ship_no = dt.Rows[i]["SHIPNO"].ToString();
                items2.Add(new ListItem(billNo, stackName, shipName, cargoName, cargoNo, ckind_no, get_owner, package, ship_no));
            }
            this.comboBox_shipname.DataSource = items2;
            this.comboBox_shipname.ValueMember = "ShipName";
            this.comboBox_shipname.DisplayMember = "DisplayName";

            string sql_lxh = "select TDH, CARGONO, CARGONAME, STACKNAME, SHIPNO, SHIPNAME, BILLNO, SHIPPER_NAME, GETOWNER  from cargo_thjh order by TDH";
            DataSet dsLXH = ws.getDataSet_Serv(sql_lxh);
            DataTable dtLXH = dsLXH.Tables[0];
            List<ListItem_LX> itemsLX = new List<ListItem_LX>();
            for (int i = 0; i < dtLXH.Rows.Count; i++)
            {
                string tdh = dtLXH.Rows[i]["TDH"].ToString();
                string cargoNo = dtLXH.Rows[i]["CARGONO"].ToString();
                string cargoName = dtLXH.Rows[i]["CARGONAME"].ToString();
                string stackName = dtLXH.Rows[i]["STACKNAME"].ToString();
                string shipNo = dtLXH.Rows[i]["SHIPNO"].ToString();
                string shipName = dtLXH.Rows[i]["SHIPNAME"].ToString();
                string billNo = dtLXH.Rows[i]["BILLNO"].ToString();
                string shipperName = dtLXH.Rows[i]["SHIPPER_NAME"].ToString();
                string getOwner = dtLXH.Rows[i]["GETOWNER"].ToString();
                itemsLX.Add(new ListItem_LX(tdh, cargoNo, cargoName, stackName, shipNo, shipName, billNo, shipperName, getOwner));
            }
            this.comboBoxLXH.DataSource = itemsLX;
            this.comboBoxLXH.ValueMember = "TDH";
            this.comboBoxLXH.DisplayMember = "DisplayName";

            this.comboBoxLXH.SelectedIndex = -1;
            this.comboBoxLXH.Text = "";
            this.comboBox_shipname.SelectedIndex = -1;
            this.comboBox_shipname.Text = "";
            this.comboBox_owner.SelectedIndex = -1;
            this.comboBox_owner.Text = "";
            this.comboBox_cargoName.SelectedIndex = -1;
            this.comboBox_cargoName.Text = "";
            this.textBox_billNO.Text = "";
            this.textBox_cargoNo.Text = "";
            this.textBox_stackName.Text = "";
            this.textBox_hc.Text = "";
            this.textBoxPackage.Text = "";
            ckingno_global = "";
            getowner_global = "";

            if (Session.CanEditLXH)
            {
                this.comboBoxLXH.Enabled = true;
                this.comboBox_shipname.Enabled = true;
            }
        }

        private void SetDefaultValue(string owner, string lxh, string shipname, string hc, string billno, string cargoname, string cargono, string stackname, string pack, string station, string comment)
        {
            //下拉选择的，如果下拉选项里没有则直接赋给Text
            SetComboBoxValue(comboBox_owner, owner);
            //this.comboBox_owner.SelectedValue = owner;
            //if (this.comboBox_owner.SelectedValue == null)
            //    this.comboBox_owner.Text = owner;
            SetComboBoxValue(comboBoxLXH, lxh);
            //this.comboBoxLXH.SelectedValue = lxh;
            //if (this.comboBoxLXH.SelectedValue == null)
            //    this.comboBoxLXH.Text = lxh;
            SetComboBoxValue(comboBox_shipname, shipname);
            //this.comboBox_shipname.SelectedValue = shipname;
            //if (this.comboBox_shipname.SelectedValue == null)
            //    this.comboBox_shipname.Text = shipname;
            SetComboBoxValue(comboBox_cargoName, cargoname);
            //this.comboBox_cargoName.SelectedValue = cargoname;
            //if (this.comboBox_cargoName.SelectedValue == null)
            //    this.comboBox_cargoName.Text = cargoname;

            this.textBox_stackName.Text = stackname;
            this.textBox_cargoNo.Text = cargono;
            this.textBox_billNO.Text = billno;
            this.textBoxPackage.Text = pack;
            this.textBox_hc.Text = hc;
            this.textBoxStation.Text = station;
            this.textBox_comment.Text = comment;
        }

        private void SetComboBoxValue(UCComboBox com, string value)
        {
            com.SelectedText = "";//先置空
            com.SelectedValue = value;
            if (com.SelectedValue == null)
                com.Text = value;
        }

        private bool check_input()
        {
            string shipper = "";
            if (this.comboBox_owner.SelectedValue != null)
                shipper = this.comboBox_owner.SelectedValue.ToString().Trim();
            else
                shipper = this.comboBox_owner.Text.ToString().Trim();
            if (string.IsNullOrEmpty(shipper))
            {
                MessageBox.Show("货主代理(发货人)不允许为空!");
                this.comboBox_owner.Focus();
                return false;
            }
            if (_hasImport)//待修改数据中有从物流网导入的
            {
                if (string.IsNullOrEmpty(this.LXH))
                {
                    MessageBox.Show("待修改数据中包含物流网导入数据，流向号不能为空！");
                    return false;
                }
                //if (string.IsNullOrEmpty(this.ShipName))
                //{
                //    MessageBox.Show("待修改数据中包含物流网导入数据，船名不能为空！");
                //    return false;
                //}
                //if (string.IsNullOrEmpty(this.HC))
                //{
                //    MessageBox.Show("待修改数据中包含物流网导入数据，航次不能为空！");
                //    return false;
                //}
                //if (string.IsNullOrEmpty(this.BillNo))
                //{
                //    MessageBox.Show("待修改数据中包含物流网导入数据，提单号不能为空！");
                //    return false;
                //}
                if (string.IsNullOrEmpty(this.CargoName))
                {
                    MessageBox.Show("待修改数据中包含物流网导入数据，货名不能为空！");
                    return false;
                }
                if (string.IsNullOrEmpty(this.CargoNo))
                {
                    MessageBox.Show("待修改数据中包含物流网导入数据，货号不能为空！");
                    return false;
                }
                if (string.IsNullOrEmpty(this.StackName))
                {
                    MessageBox.Show("待修改数据中包含物流网导入数据，垛名不能为空！");
                    return false;
                }
                //if (string.IsNullOrEmpty(this.Pack))
                //{
                //    MessageBox.Show("待修改数据中包含物流网导入数据，包装不能为空！");
                //    return false;
                //}
            }
            return true;
        }

        #region===========press event handler===========

        private void comboBox_owner_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.comboBoxLXH.Focus();
            }
        }

        private void comboBox_owner_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboBoxLXH.Focus();
        }

        private void textBox_hc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.comboBox_cargoName.Focus();
            }
        }

        private void textBox_billNO_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.comboBox_cargoName.Focus();
            }
        }

        private void comboBox_cargoName_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textBox_comment.Focus();
        }

        private void comboBox_cargoName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_comment.Focus();
            }
        }

        private void textBox_stackName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBoxPackage.Focus();
            }

        }

        private void textBoxPackage_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBoxStation.Focus();
            }
        }

        private void textBoxStation_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_comment.Focus();
            }
        }

        #endregion

        string ckingno_global = "";
        string getowner_global = "";
        string shipno_global = "";

        private void comboBox_shipname_DropDownClosed(object sender, EventArgs e)
        {
            ShipNameChanged();
        }

        private void comboBox_shipname_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShipNameChanged();
        }

        private void ShipNameChanged()
        {
            if (this.comboBox_shipname.SelectedIndex >= 0 && !bLXHisChanged)
            {
                ListItem selectedItem = (ListItem)this.comboBox_shipname.SelectedItem;
                this.textBox_billNO.Text = selectedItem.BillNO;
                SetComboBoxValue(comboBox_cargoName, selectedItem.CargoName);
                //this.comboBox_cargoName.SelectedValue = selectedItem.CargoName;
                this.textBox_cargoNo.Text = selectedItem.CargonNO;
                this.textBox_stackName.Text = selectedItem.StackName;
                ckingno_global = selectedItem.CkindNO;
                getowner_global = selectedItem.GetOwner;
                this.textBoxPackage.Text = selectedItem.Package;
                shipno_global = selectedItem.ShipNO;
            }
        }

        bool bLXHisChanged = false;

        private void comboBoxLXH_DropDownClosed(object sender, EventArgs e)
        {
            LXHChanged();
        }

        private void comboBoxLXH_SelectedIndexChanged(object sender, EventArgs e)
        {
            LXHChanged();
        }

        private void LXHChanged()
        {
            if (this.comboBoxLXH.SelectedIndex >= 0)
            {
                ListItem_LX selectedItem = (ListItem_LX)this.comboBoxLXH.SelectedItem;
                this.textBox_billNO.Text = selectedItem.BILLNO;
                SetComboBoxValue(comboBox_cargoName, selectedItem.CARGONAME);
                //this.comboBox_cargoName.SelectedValue = selectedItem.CARGONAME;
                this.textBox_cargoNo.Text = selectedItem.CARGONO;
                this.textBox_stackName.Text = selectedItem.STACKNAME;
                try
                {
                    bLXHisChanged = true;
                    SetComboBoxValue(comboBox_shipname, selectedItem.SHIPNAME);
                    //this.comboBox_shipname.SelectedValue = selectedItem.SHIPNAME;
                    //if (comboBox_shipname.SelectedValue == null)
                    //    this.comboBox_shipname.Text = selectedItem.SHIPNAME;
                }
                finally
                {
                    bLXHisChanged = false;
                }
                getowner_global = selectedItem.GETOWNER;
                shipno_global = selectedItem.SHIPNO;
                this.textBox_hc.Text = selectedItem.SHIPNO;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (check_input())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
    }
}
